package com.technology.education.ui.student.black_list

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.dslItem
import com.angcyo.dsladapter.removeItem
import com.angcyo.dsladapter.toDslItemList
import com.google.android.material.imageview.ShapeableImageView
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.TeacherInfoModel
import com.technology.education.ext.header
import com.technology.education.ext.navigation
import com.technology.education.ext.setPageItems
import com.technology.education.widget.CommonButton
import me.hgj.jetpackmvvm.ext.parseState

/**
 *@author:create by BZY
 *Date: 2022/3/11 08
 * 教师黑名单列表
 */
class BlackListFragment : BaseRecyclerTopBarFragment<BlackListViewModel>() {
    override fun initViews() {
        setTitle("黑名单")
    }

    override fun lazyLoadData() {
        loading()
    }

    private fun setUI(list: List<TeacherInfoModel>) {
        renderAdapter {
            setPageItems(list.toDslItemList(R.layout.item_follow_teacher) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemData?.let {
                        if (it is TeacherInfoModel) {
                            itemTag = it.id
                            itemHolder.tv(R.id.tv_name)?.text = it.name
                            itemHolder.v<ShapeableImageView>(R.id.iv_header)?.apply {
                                header(it.avatar, this)
                            }
                            itemHolder.v<CommonButton>(R.id.btn_call)?.text = "移除"
                            itemHolder.click(R.id.btn_call) { _ ->
                                mViewModel.delBlack(it.id)
                            }
                        }
                    }

                }
            }, mViewModel.pageIndex)
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { data ->
                hasMore(data.total)
                setUI(data.list)
            }, onError = {
                loadError()
            })
        }

        mViewModel.delResult.observe(this) {
            renderAdapter {
                removeItem(it)
            }
        }
    }
}